package org.halk.job_client.mq.produce;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.halk.job_client.component.JobContext;
import org.halk.job_client.pojo.entity.JobResponse;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @Author haozhu7
 * @Date 2024/9/27 22:33
 **/
@Slf4j
@Component
public class JobProduce {

    public static final String HALK_JOB_RESULT = "HALK_JOB_RESULT";

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @PostConstruct
    public void regCallback() {
        JobContext.setJobProduce(this);
    }

    public void sendJobMsg(JobResponse msg) {

        rocketMQTemplate.asyncSend(HALK_JOB_RESULT, new GenericMessage<>(msg), new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                log.info("sendJobMsg onSuccess sendResult: {}", sendResult);
            }

            @Override
            public void onException(Throwable throwable) {
                log.info("sendJobMsg onException throwable: {}", throwable);
            }
        });

    }
}
